﻿Pui.header1 = {
    init: function() {
        this.Main()
    },
    Main: function() {
        this.SelectEvents()
    },
    OnLoad: function() {
        var defalt_val = '搜一下';
        $('#SearchInput').val(defalt_val).focus(function() {
            var val = $(this).val();
            if (val == defalt_val) {
                $(this).val('')
            }
        }).blur(function() {
            var val = $(this).val();
            if (val == '') {
                $(this).val(defalt_val)
            }
        })
    },
    SelectEvents: function() {
        $('body').click(function() {
            if ($('#SelDiag').css('display') != 'none') {
                $('#SelDiag').slideUp('fast')
            }
        });
        $('.btnSel a').click(function() {
            if ($('#SelDiag').css('display') != 'none') {
                $('#SelDiag').slideUp('fast')
            } else {
                $('#SelDiag').slideDown('fast')
            }
            return false
        });
        $('#SelDiag1').click(function() {
            Pui.header1.SelectDiag('video', $(this));
            this.blur();
            return false
        });
        $('#SelDiag2').click(function() {
            Pui.header1.SelectDiag('user', $(this));
            this.blur();
            return false
        });
        $('#SelDiag3').click(function() {
            Pui.header1.SelectDiag('subject', $(this));
            this.blur();
            return false
        })
    },
    SelectDiag: function(searchType, href) {
        $('#SearchType').val(searchType);
        $('#SelDiag').css('display', 'none');
        $('#Selected').text($(href).text());
        $('#SearchT').focus()
    },
    DoSearch: function(o) {
        var val = $(o).find('#SearchInput').val();
        if (val == '' || val == '搜一下') {
            alert('对不起，关键词不能为空，请重新输入');
            $('#SearchInput').focus();
            return false
        } else {
            var sdi = o.searchDomain;
            var sd = "http://search.pomoho.com";
            if (sdi != undefined && sdi.value != "") {
                sd = sdi.value
            }
            var key = encodeURIComponent(val);
            var url = sd + "/" + o.searchType.value + "search.aspx?_keyword=" + key;
            if (document.location.hostname.indexOf("search.pomoho.com") != -1) {
                document.location.href = url
            } else {
                window.open(url, 'search')
            }
            return false
        }
    },
    Search: function(val) {
        if (val == '') {
            return false
        } else {
            var sd = "http://search.pomoho.com";
            var key = encodeURIComponent(val);
            var url = sd + "/videosearch.aspx?_keyword=" + key;
            if (document.location.hostname.indexOf("search.pomoho.com") != -1) {
                document.location.href = url
            } else {
                window.open(url, 'search')
            }
            return false
        }
    }
};
$(function() {
    Pui.header1.init()
});
Pui.header = Pui.header_v2 = Pui.newSH = {
    init: function() {
        _user = {
            uid: 0,
            nick: 'onemax',
            spacename: '',
            newmsg: 0
        };
        _is_login = false;
        var n = Pui.cookie('LoginInfo');
        if (n) {
            var arr = n.split('$***$');
            _user = {
                uid: arr[0].replace('LoginInfo=', ''),
                nick: arr[1],
                spacename: arr[2],
                newmsg: 0,
                spaceurl: arr[3]
            };
            _is_login = true;
            this.islogin()
        } else {
            this.unlogin()
        };
        this.doSearch();
        this.router()
    },
    router: function() {
        var pageType = "";
        var n = null;
        var bodyID = document.body.id.toLowerCase();
        var hostName = window.location.hostname;
        if (bodyID != "") {
            if (bodyID == "b-index") {
                pageType = "index";
                n = Pui.cookie('router')
            } else {
                if (bodyID.lastIndexOf('-') > 1 && bodyID.indexOf('-index') > 2) {
                    pageType = bodyID.substr(bodyID.indexOf('b-') + 2, bodyID.indexOf('-index') - 2);
                    if (pageType == "car") {
                        pageType = "auto"
                    }
                    n = Pui.cookie('router')
                }
            }
        }
        if (n != null) {
            if (n == "router=true") {
                if (pageType == "index") {
                    window.location = "http://pomoho.gogochannel.com/"
                } else {
                    window.location = "http://pomoho.gogochannel.com/" + pageType
                }
            }
        }
    },
    checkrouter: function() {
        var pageType = "";
        var bodyID = document.body.id.toLowerCase();
        var hostName = window.location.hostname;
        if (bodyID != "") {
            if (bodyID == "b-index") {
                pageType = "index";
                Pui.loadScript("http://www.pomoho.com/do/interfaces/router.aspx?pagetype=" + pageType + "");
                this.router()
            } else {
                if (bodyID.lastIndexOf('-') > 1 && bodyID.indexOf('-index') > 2) {
                    pageType = bodyID.substr(bodyID.indexOf('b-') + 2, bodyID.indexOf('-index') - 2);
                    if (pageType == "car") {
                        pageType = "auto"
                    }
                    Pui.loadScript("http://www.pomoho.com/do/interfaces/router.aspx?pagetype=" + pageType + "");
                    this.router()
                }
            }
            if (document.location.hash == "#test") {
                alert("bodyID=" + bodyID + ",pageType=" + pageType)
            }
        }
    },
    unlogin: function() {
        var str = '';
        str += '<li class="s"><a target="_blank" class="f" title="返回首页" href="http://www.pomoho.com/">返回首页</a></li>';
        str += '<li class="s"><a class="f" title="登录" href="javascript:Pui.header.loginfo()">登录</a></li>';
        str += '<li class="s logout"><a class="f" title="注册" href="http://www.pomoho.com/ui/reg.aspx" target="_blank">注册</a></li>';
        $("#TopNav").html(str)
    },
    islogin: function() {
        var t_time = this.localTime();
        var str = '';
        str += '<ul id="TopNav">';
        str += '<li class="s" >' + t_time + '，<a class="user" target="_blank" href="http://home.pomoho.com/' + _user.spaceurl + '.shtml" title="' + _user.nick + '" >' + Pui.header.SetString(_user.nick, 10) + '</a></li>';
        str += '<li class="s"><a class="count" title="你的帐户" href="http://www.pomoho.com/manage/index.aspx" target="_blank" >帐户</a>';
        str += '   <div class="d s3">';
        str += '     <ul>';
        str += '       <li><a class="d_item" title="我的视频" href="http://www.pomoho.com/manage/managevideo.aspx" target="_blank" >我的视频</a></li>';
        str += '       <li><a class="d_item" title="我的订阅" href="http://www.pomoho.com/manage/subscribevblog.aspx" target="_blank" >我的订阅</a></li>';
        str += '       <li><a class="d_item" title="我的收藏" href="http://www.pomoho.com/manage/managevideo.aspx?type=favs" target="_blank" >我的收藏</a></li>';
        str += '       <li><a class="d_item" title="我的相册" href="http://www.pomoho.com/manage/managealbum.aspx" target="_blank" >我的相册</a></li>';
        str += '     </ul>';
        str += '     <div class="m1"><a title="更多管理" href="http://www.pomoho.com/manage/index.aspx" target="_blank" >更多..&nbsp;&nbsp;</a></div>';
        str += '   </div>';
        str += ' </li>';
        str += ' <li class="s"><a class="msg" title="你的消息" href="http://www.pomoho.com/manage/messagelist.aspx?type=1" target="_blank" >消息</a><span>(0)</span>';
        str += '   <div class="d s2">';
        str += '     <div class="title"><div class="d_video">主题</div><div class="d_author">消息来源</div></div>';
        str += '     <ul>';
        str += '     </ul>';
        str += '     <div class="m"><a title="全部消息" href="http://www.pomoho.com/manage/messagelist.aspx?type=1" target="_blank" >全部消息&gt;&gt;</a></div>';
        str += '   </div>';
        str += ' </li>';
        str += ' <li class="logout"><a title="退出登录" href="http://www.pomoho.com/ui/logout.aspx?AfterLoginReturnURL=' + window.location + '" >退出</a></li>';
        str += ' </ul>';
        $("#L").removeClass().html(str);
        this.SH();
        Pui.loadScript("http://notice.pomoho.com/loadmessage2.0.aspx")
    },
    SH: function() {
        var tim;
        $("#TopNav .s").hover(function() {
            $(this).siblings().find(".d").hide();
            $(this).find(".d").show();
            clearTimeout(tim)
        },
        function() {
            tim = setTimeout(overDiv, 500);
            function overDiv() {
                $("#TopNav").find(".d").slideUp()
            }
        })
    },
    localTime: function() {
        var time = new Date();
        var hour = time.getHours();
        var t_time = ($.browser.msie) ? String(time).split(" ")[3].substr(0, 5) : String(time).split(" ")[4].substr(0, 5);
        switch (hour) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            t_time = '早上好';
            break;
        case 8:
        case 9:
        case 10:
        case 11:
            t_time = '上午好';
            break;
        case 12:
            t_time = '中午好';
            break;
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
            t_time = '下午好';
            break;
        case 18:
        case 19:
        case 20:
        case 21:
        case 22:
        case 23:
            t_time = '晚上好';
            break
        }
        return t_time
    },
    Msginit: function(jsonMsg) {
        var str = '';
        var len = jsonMsg.length;
        if (len <= 1) {
            var str = '<li style="width:100%">您还没有消息</li>'
        } else {
            for (var i = 1; i <= len - 1; i++) {
                var msgtypename;
                switch (jsonMsg[i].msgtype) {
                case 1:
                    msgtypename = "站内消息"
                    break;
                case 2:
                    msgtypename = "系统消息"
                    break
                case 3:
                    msgtypename = "公告"
                    break
                }
                str += '<li id="msg' + i + '">';
                if (jsonMsg[i].msgtype != 3) {
                    str += '<div class="d_video"><a title="' + jsonMsg[i].msgtitle + '" href="http://www.pomoho.com/manage/readmessage.aspx?receiveid=' + jsonMsg[i].msgid + '&rettype=1" target="_blank" >' + jsonMsg[i].msgtitle + '</a></div>'
                } else {
                    str += '<div class="d_video"><a title="' + jsonMsg[i].msgtitle + '" href="http://www.pomoho.com/manage/readmessage.aspx?bulletinid=' + jsonMsg[i].msgid + '&rettype=1" target="_blank" >' + jsonMsg[i].msgtitle + '</a></div>'
                }
                str += '<div class="d_author">' + msgtypename + '</div>';
                str += '<a class="d_close" title="删除消息" href="javascript:Pui.header.Msgdel(' + jsonMsg[i].msgid + ',' + jsonMsg[i].msgtype + ',' + i + ')" ></a> </li>'
            }
        }
        $("#TopNav .s2 ul").html(str);
        $("#TopNav .s2").parent().find("span").html("(" + jsonMsg[0] + ")")
    },
    Msgdel: function(msgid, msgtype, num) {
        $("#msg" + num).remove();
        var Msgurl = "http://notice.pomoho.com/readmessage.aspx?messageID=" + msgid + "&Userid=" + _user.uid + "&messageType=" + msgtype;
        Pui.loadScript(Msgurl);
        var num = parseInt($("#TopNav .s2").parent().find("span").html().substr(1)) - 1;
        if (num <= 0) num = 0;
        $("#TopNav .s2").parent().find("span").html("(" + num + ")")
    },
    doSearch: function(e) {
        if (typeof(s) != 'undefined') return
        else s = 1;
        $("#goSearch").click(function() {
            var values = $.trim($("#searchGame").val());
            if (!values) {
                alert("关键字不能为空");
                $("#searchGame").focus()
            } else {
                var param = encodeURIComponent(values.replace(/[\/_]/g, ' '));
                var url = "http://search.pomoho.com/videosearch.aspx?_keyword=" + param;
                window.open(url, 'search')
            }
        })
    },
    logDiv: function(sType, sTarget, sUrl, callback) {
        var str = '';
        if ($.browser.msie && String($('body').attr('id')) == 'B-Upload') {
            str += ''
        } else {
            if ($.browser.msie) {
                str += '<iframe id="zhezhao"></iframe>'
            } else {
                str += '<div id="zhezhao"></div>'
            }
        }
        str += '	<div id="Prompt" class="prompt">';
        str += '		  <div class="k log_v">';
        str += '		    <div class="meat">';
        str += '		      <div class="close"><img alt="关闭" src="http://static01.pomoho.com/themes/site/default/images/games/close.gif" width="16" height="16" /></div>';
        str += '		      <h3></h3>';
        str += '		      <p class="p2">您的帐号：';
        str += '		        <input id="uidName" type="text" onkeyup="Pui.header.submitF(event)" />';
        str += '		      </p>';
        str += '		      <p class="p2">登录密码：';
        str += '		        <input id="uidPwd" type="password" onkeyup="Pui.header.submitF(event)" />';
        str += '		        <a href="http://www.baomihua.com/ui/findpassword.aspx" target="_blank">忘记密码</a></p>';
        str += '		      <p class="pbt">';
        str += '		        <input id="btnlog" class="log" type="submit" value="登录" />';
        str += '		        <input id="reg" class="log" type="button" value="注册" />';
        str += '		      </p>';
        str += '		    </div>';
        str += '		  </div>';
        str += '		</div>';
        $("body").prepend(str);
        if ($.browser.msie && String($('body').attr('id')) == 'B-Upload') {
            Pra.ui.overlay()
        } else {
            if ($.browser.msie) {
                var cw = window.frames['zhezhao'];
                cw.document.open();
                cw.document.write('<style type="text/css">body{background:#000;}</style>');
                cw.document.close()
            }
        }
        $("#Prompt .close img").bind("click", Pui.header.closeDiv);
        $("#btnlog").click(function() {
            Pui.header.login(sType, sTarget, sUrl, callback)
        });
        $("#reg").click(function() {
            window.open('http://www.baomihua.com/ui/reg.aspx')
        })
    },
    showLogDiv: function(sType, sTarget, sUrl, callback) {
        if (String($('body').attr('id')) == 'B-Upload') {
            Pui.header.logDiv(sType, sTarget, sUrl, callback)
        } else if (!$("#Prompt").length) {
            Pui.header.logDiv(sType, sTarget, sUrl, callback)
        }
        var doc = document;
        var docElement = doc.documentElement;
        var sHeight = docElement.clientHeight;
        var dH = docElement.scrollHeight > sHeight ? docElement.scrollHeight: sHeight;
        var scH = docElement.scrollTop >= 0 ? (docElement.scrollTop + sHeight / 2) : sHeight;
        var h = scH - $("#Prompt").height() / 2;
        var w = docElement.clientWidth / 2 - $("#Prompt").width() / 2;
        $("#zhezhao").height(dH).show();
        $("#Prompt").css({
            top: h,
            left: w
        })
    },
    loginfo: function(sType, sTarget, sUrl, callback) {
        Pui.header.showLogDiv(sType, sTarget, sUrl, callback);
        $("#Prompt").show();
        $("#Prompt .k")[0].className = "k log_v";
        $('#uidName').focus();
        if (document.attachEvent) {
            document.attachEvent('onkeyup', Pui.header.EXC)
        } else {
            document.addEventListener('keyup', Pui.header.EXC, false)
        }
    },
    loginfo2: function(callback, sType, sTarget, sUrl) {
        this.loginfo(sType, sTarget, sUrl, callback)
    },
    loginfo2_callback: function(json) {
        window.location.href = "http://www.pomoho.com/manage/index.aspx"
    },
    submitF: function(e) {
        var event = window.event || e;
        if (event.keyCode == 13) $("#btnlog").click()
    },
    EXC: function(e) {
        var event = window.event || e;
        if (event.keyCode == 27) Pui.header.closeDiv()
    },
    login: function(sType, sTarget, sUrl, callback) {
        var userName = $.trim($("#uidName").val());
        var pwd = $("#uidPwd").val();
        if (userName == '' || pwd == '') {
            alert("用户名或者密码不能为空")
        } else {
            if (sType || sUrl) {
                Pui.ui.logDiagAjax(userName, pwd, sType, sTarget, sUrl, callback)
            } else {
                Pui.header.logAjax(userName, pwd, callback)
            }
        }
    },
    closeDiv: function() {
        if ($.browser.msie && String($('body').attr('id')) == 'B-Upload') {
            Pra.ui.overlay()
        } else if (String($('body').attr('id')) == 'B-Upload') {
            $("#zhezhao").remove()
        } else {
            $("#zhezhao").hide()
        }
        $("#Prompt").hide()
    },
    logAjax: function(userName, pwd, callback) {
        var urlRoot = window.location.hostname;
        var urlString = window.location.toString().toLowerCase();
        var urlstr;
        if (urlString.indexOf("video.pomoho.com/") == -1) {
            urlstr = "/do/interfaces/sendajaxreq.aspx"
        } else {
            urlstr = "/sendajaxreq.aspx"
        }
        $.ajax({
            url: "http://www.pomoho.com/api/userapi.asmx/UserLogin?jsoncallback=?",
            dataType: "json",
            data: {
                userName: userName,
                password: pwd
            },
            error: function() {
                alert('可能网络问题,登录失败,请重试!');
                Pui.header.closeDiv()
            },
            success: function(result) {
                var userlog = eval(result);
                switch (parseInt(userlog.Result)) {
                case - 1 : alert("登录帐号不存在");
                    break;
                case - 2 : alert("用户名或者密码错误,请重试");
                    break;
                case 1:
                    Pui.header.cookie('UserInfo', userlog.UserInfo, {
                        path: '/',
                        domain: '.baomihua.com',
                        expires: 1 / 6
                    });
                    Pui.header.cookie('LoginInfo', userlog.LoginInfo, {
                        path: '/',
                        domain: '.baomihua.com',
                        expires: 1 / 6
                    });
                    var urlString = window.location.toString().toLowerCase();
                    if (urlString.indexOf("pomoho.com/combo") != -1 && (typeof(flag) == 'undefined' || flag == null)) {
                        window.location.reload();
                        return
                    }
                    Pui.header.init();
                    if (String($('body').attr('id')) == 'B-Upload' && document.getElementById("hid_n").value == "true" && (typeof(flag) == 'undefined' || flag == null)) {
                        document.getElementById("FrameUpload").contentWindow.Pui.upload.unite('GoTo2');
                        document.getElementById("FrameUpload").contentWindow.start_upload(document.getElementById("FrameUpload").contentWindow.$('#file1').val(), document.getElementById("FrameUpload").contentWindow.$('#uploadForm_Title').val());
                        if (document.getElementById("FrameUpload").contentWindow.$('#hid_ver').val() == 'family') {
                            document.getElementById("FrameUpload").contentWindow.$('#Upload_2 .s4').addClass('none');
                            document.getElementById("FrameUpload").contentWindow.$('#Upload_2 .s5').addClass('none');
                            $('#Layout_1').height('650px')
                        } else {
                            $('#Layout_1').height('700px')
                        }
                    }
                    if (typeof(flag) == 'undefined' || flag == null) Pui.header.closeDiv();
                    else if (flag == 1) {
                        Pui.user.haveFriend();
                        flag = null
                    } else if (flag == 2) {
                        Pui.loadScript("http://www.pomoho.com/do/video/dealajaxdata.aspx?itype=2&imdbid=" + flvid);
                        flag = null;
                        Pui.header.closeDiv()
                    } else if (flag == 3) {
                        f_addfriend(_userid);
                        flag = null;
                        Pui.header.closeDiv()
                    } else if (flag == 4) {
                        flag = null;
                        Pui.header.closeDiv();
                        f_comboInterFace(addPram.otype, addPram.atype, addPram.comboid, _user.uid);
                        Pui.header.closeDiv()
                    } else if (flag == 5) {
                        flag = null;
                        Pui.ReUrl(pathUrl);
                        Pui.header.closeDiv()
                    } else if (flag == 6) {
                        flag = null;
                        Pui.header.closeDiv();
                        DoMovieAction(_subjectID, _emitStatus)
                    }
                    if ('undefined' != typeof(callback)) {
                        callback()
                    }
                    break;
                default:
                    alert("系统错误,请重试，错误代码：" + userlog[1].ErrMsg);
                    Pui.header.closeDiv()
                }
            }
        })
    },
    cookie: function(name, value, options) {
        if (typeof(value) != 'undefined') {
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000))
                } else {
                    date = options.expires
                }
                expires = '; expires=' + date.toUTCString()
            }
            var path = options.path ? '; path=' + options.path: '';
            var domain = options.domain ? '; domain=' + options.domain: '';
            var secure = options.secure ? '; secure': '';
            document.cookie = [name, '=', name + '=' + value, expires, path, domain, secure].join('')
        } else {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = unescape(cookie.substring(name.length + 1));
                        break
                    }
                }
            }
            return cookieValue
        }
    },
    SetString: function(str, len) {
        var strlen = 0;
        var s = "";
        for (var i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 128) strlen += 2;
            else strlen++;
            s += str.charAt(i);
            if (strlen >= len) return s
        }
        return s
    }
};
if (!Pui.msg) {
    Pui.msg = {};
};
Pui.msg.readMessageHandler = function(a) {
    var a = /pomoho.com\/manage\/readmessage.aspx/;
    var b = window.location.href.toString();
    if (a.test(b)) {
        alert("操作成功！");
    } else {}
};

